package com.onetime.mall.trading.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onetime.mall.trading.dao.FavoriteDao;
import com.onetime.mall.trading.entity.FavoriteEntity;
import com.onetime.mall.trading.entity.IdleItemEntity;
import com.onetime.mall.trading.service.FavoriteService;
import com.onetime.mall.trading.service.IdleItemService;
import com.onetime.mall.trading.utils.PageUtils;
import com.onetime.mall.trading.utils.Query;
import com.onetime.mall.trading.vo.FavoriteProductsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("favoriteService")
public class FavoriteServiceImpl extends ServiceImpl<FavoriteDao, FavoriteEntity> implements FavoriteService {

    @Autowired
    IdleItemService idleItemService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FavoriteEntity> page = this.page(
                new Query<FavoriteEntity>().getPage(params),
                new QueryWrapper<FavoriteEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 新增收藏
     *
     * @param favoriteEntity
     * @return
     */
    @Override
    public boolean addFavorite(FavoriteEntity favoriteEntity) {
        return baseMapper.insert(favoriteEntity) == 1;
    }

    /**
     * 判断用户是否收藏某个闲置
     *
     * @param userId 用户ID
     * @param idleId 商品ID
     * @return 收藏表id
     */
    @Override
    public Integer isFavorite(Long userId, Long idleId) {
        QueryWrapper<FavoriteEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("idle_id", idleId);
        FavoriteEntity favoriteEntity = baseMapper.selectOne(wrapper);
        if (favoriteEntity != null) {
            return favoriteEntity.getId().intValue();
        } else {
            return null;
        }
    }

    /**
     * 删除收藏
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteFavorite(Long id) {
        return baseMapper.deleteById(id) == 1;
    }


    /**
     * 查询一个用户的所有收藏
     *
     * @param userId
     * @return
     */
    @Override
    public List<FavoriteProductsVo> getAllFavorite(Long userId) {
        QueryWrapper<FavoriteEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<FavoriteEntity> favoriteEntities = baseMapper.selectList(wrapper);
        if (favoriteEntities.size() > 0) {
            List<FavoriteProductsVo> collect = favoriteEntities.stream().map(item -> {
                FavoriteProductsVo vo = new FavoriteProductsVo();
                BeanUtils.copyProperties(item, vo);

                Long idleId = item.getIdleId();
                IdleItemEntity idleItem = idleItemService.getById(idleId);

                vo.setIdleItemEntity(idleItem);
                return vo;
            }).collect(Collectors.toList());
            return collect;
        } else {
            return null;
        }

    }

}